//返回上一级
function previous() {
    window.location = "../policyList.html?type=" + type + "&id=" + DepotID + "&name=" + base64.encode(DepotName) + "";
}
//获取url参数
function getQueryString(name) {
    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
    var r = window.location.search.substr(1).match(reg);
    if (r != null)
        return unescape(r[2]);
    return null;
}
function generateParameter() {
    var obj = {};
    //obj.policy_desc = $(".policy_desc").val();
    obj.Server_IP = {
        IPType: "IPv4",
        IPv4Address: $(".Server_IP").val()
    };
    obj.Server_Port = $(".Server_Port").val();
    obj.UserName = $(".UserName").val();
    obj.Password = $(".Password").val();
    obj.ANT_FileName = $(".ANT_FileName").val();
    obj.ANT_SerialNo = "0";
    return obj;
}
function showData() {
    $.ajax({
        type: "POST",
        async: false,
        url: "../../action/policy/get.do",
        data: {type: type, messageNo: id, instance: DepotName},
        dataType: "json",
        success: function(souData) {
            //souData = $.parseJSON(souData);
            var policy_desc = souData.Comment;
            $(".policy_desc").val(policy_desc);
            var data = souData.Content;
            $(".Server_Port").val(data.Server_Port);
            $(".UserName").val(data.UserName);
            $(".Password").val(data.Password);
            $(".ANT_FileName").val(data.ANT_FileName);
            $(".Server_IP").val(data.Server_IP.IPv4Address);
        }
    });
}
function save() {
    var form = $(".form-horizontal").validate();
    if (!form.form()) {
        return;
    }
    var comment = $(".policy_desc").val();
    var policy = generateParameter();
    $.ajax({url: "../../action/system/setproperty.do", async: false, type: 'POST', data: {property: "apptype.appname.name", value: policy.ANT_FileName}});
    $.ajax({
        type: "POST",
        async: false,
        url: "../../action/policy/create.do",
        data: {comment: comment, policy: JSON.stringify(policy), type: type, instance: DepotName},
        dataType: "json",
        success: function(data) {
            if (data.result == "true") {
                alert("添加成功！");
                previous();
            } else {
                alert("添加失败!")
            }
        }
    });
}
//编辑数据
function edit() {
    var form = $(".form-horizontal").validate();
    if (!form.form()) {
        return;
    }
    var comment = $(".policy_desc").val();
    var policy = generateParameter();
    $.ajax({url: "../../action/system/setproperty.do", async: false, type: 'POST', data: {property: "apptype.appname.name", value: policy.ANT_FileName}});
    $.ajax({
        type: "POST",
        async: false,
        url: "../../action/policy/update.do",
        data: {messageNo: id, comment: comment, policy: JSON.stringify(policy), type: type, instance: DepotName},
        dataType: "json",
        success: function(data) {
            if (data.result == "true") {
                alert("编辑成功！");
                previous();
            } else {
                alert("编辑失败!")
            }
        }
    });
}
var base64 = new Base64();
var type = getQueryString("type");
var DepotID = getQueryString("DepotID");
var DepotName = getQueryString("DepotName");
DepotName = base64.decode(DepotName);
var id = getQueryString("id");
var appUrl = parent.appUrl;
var appName = parent.appName;
var test = parent.test;
$(function() {
//    console.log("type:" + type + "\tDepotID:" + DepotID + "\tDepotName:" + DepotName + "\tid:" + id);
    $(".header").text(DepotName + "  策略管理");
});
/*
 * 查看旧版本的策略内容
 */
function show_oldPolicy(array, number) {
    var souData = array[number];
    $(".header").text("当前策略在" + souData.CreateTime + "创建,ID为" + souData.MessageNo + ",版本号为" + souData.MessageSerialNo + ",属于" + souData.Instance + "策略库");

    var policy_desc = souData.Comment;
    $(".policy_desc").val(policy_desc);
    var data = souData.Content;
    $(".Server_Port").val(data.Server_Port);
    $(".UserName").val(data.UserName);
    $(".Password").val(data.Password);
    $(".ANT_FileName").val(data.ANT_FileName);
    $(".Server_IP").val(data.Server_IP.IPv4Address);
}

/*
 *获取所有版本的策略内容 
 */
function getAllPolicy() {
    $.ajax({
        type: "GET",
        async: false,
        url: "../../action/policy/history_policy_info.do",
        data: {type: type, instance: getQueryString("DepotName"), messageNo: id},
        dataType: "json",
        success: function(data) {
            policyArray = data;
            policyAmount = data.length
        }
    });
}